package com.yaj.myfactorysupport.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yaj.myfactorysupport.entity.VTmLine;
import com.yaj.myfactorysupport.mapper.VTmLineMapper;
import com.yaj.myfactorysupport.service.VTmLineService;
import com.yaj.myfactorysupport.util.ErrorCodeEnum;
import com.yaj.myfactorysupport.util.RespBean;
import com.yaj.myfactorysupport.vo.TmLineVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wei
 * @since 2021-05-31
 */
@Service
public class VTmLineServiceImpl extends ServiceImpl<VTmLineMapper, VTmLine> implements VTmLineService {
    @Resource
    private VTmLineMapper vTmLineMapper;

    @Override
    public RespBean getLineList() {
        List<VTmLine> vTmLines = vTmLineMapper.selectList(null);
        List<TmLineVo> tmLineVos = new ArrayList<TmLineVo>();

        if(ObjectUtil.isNull(vTmLines)){
            return RespBean.error(ErrorCodeEnum.无法取得产线一览.getCode(),ErrorCodeEnum.无法取得产线一览.getMsg());
        }

        for(VTmLine vTmLine:vTmLines){
            TmLineVo tmLineVo = new TmLineVo();
            BeanUtil.copyProperties(vTmLine,tmLineVo);
            tmLineVos.add(tmLineVo);
        }
        //对产线号排序
        tmLineVos.sort(Comparator.comparing(TmLineVo::getLineId));
        return RespBean.success(tmLineVos);
    }

    @Override
    public RespBean getLineNameByLineId(String lineId) {
        VTmLine vTmLine = vTmLineMapper.selectOne(new QueryWrapper<VTmLine>().lambda().eq(VTmLine::getLineId,lineId));

        if(ObjectUtil.isNull(vTmLine)){
            return RespBean.error(ErrorCodeEnum.没有找到对应的产线.getCode(),ErrorCodeEnum.没有找到对应的产线.getMsg());
        }
        TmLineVo tmLineVo = new TmLineVo();
        BeanUtil.copyProperties(vTmLine,tmLineVo);

        return RespBean.success(tmLineVo);
    }
}
